﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;
using Windows8Accelerator.Core.Xaml.Controls;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class SuperTextFormatHelper : SuperTextFormatHelperBase
    {
        public override void Format(SuperTextFormat textFormat, object control)
        {
            if (control is Control)
                FormatControl(textFormat, (Control)control);
            else if (control is RichTextBlock)
                FormatRichText(textFormat, (RichTextBlock)control);
            else if (control is TextElement)
                FormatTextElement(textFormat, (TextElement)control);
        }

        public override SuperTextFormat GetFormat(object control)
        { 
            if (control is Control)
                return GetControlFormat((Control)control);
            else if (control is RichTextBlock)
                return GetRichTextBlockFormat((RichTextBlock)control);
            else if (control is TextElement)
                return GetTextElementFormat((TextElement)control);
            return  null;
        }

        public static void FormatTextElement(SuperTextFormat textFormat, TextElement textElement)
        {
            if (textFormat != null)
            {
                if (textFormat.FontWeight.HasValue)
                    textElement.FontWeight = ConvertToFontWeight(textFormat.FontWeight.Value);
                if (textFormat.FontSize.HasValue)
                    textElement.FontSize = textFormat.FontSize.Value;
                if (textFormat.FontStyle.HasValue)
                    textElement.FontStyle = ConvertToFontStyle(textFormat.FontStyle.Value);
                if (!string.IsNullOrEmpty(textFormat.FontFamilyName))
                    textElement.FontFamily = new FontFamily(textFormat.FontFamilyName);
                if (!string.IsNullOrEmpty(textFormat.ForegroundColor))
                    textElement.Foreground = new SolidColorBrush(ColorHelperEx.StringToColor(textFormat.ForegroundColor));
            }
        }

        public static void FormatRichText(SuperTextFormat textFormat, RichTextBlock richTextBlock)
        {
            if (textFormat != null)
            {
                if (textFormat.FontWeight.HasValue)
                    richTextBlock.FontWeight = ConvertToFontWeight(textFormat.FontWeight.Value);
                if (textFormat.FontSize.HasValue)
                    richTextBlock.FontSize = textFormat.FontSize.Value;
                if (textFormat.FontStyle.HasValue)
                    richTextBlock.FontStyle = ConvertToFontStyle(textFormat.FontStyle.Value);
                if (!string.IsNullOrEmpty(textFormat.FontFamilyName))
                    richTextBlock.FontFamily = new FontFamily(textFormat.FontFamilyName);
                if (!string.IsNullOrEmpty(textFormat.ForegroundColor))
                    richTextBlock.Foreground = new SolidColorBrush(ColorHelperEx.StringToColor(textFormat.ForegroundColor));
            }
        }

        public static void FormatControl(SuperTextFormat textFormat, Control control)
        {
            if (textFormat != null)
            {
                if (textFormat.FontWeight.HasValue)
                    control.FontWeight = ConvertToFontWeight(textFormat.FontWeight.Value);
                if (textFormat.FontSize.HasValue)
                    control.FontSize = textFormat.FontSize.Value;
                if (textFormat.FontStyle.HasValue)
                    control.FontStyle = ConvertToFontStyle(textFormat.FontStyle.Value);
                if (!string.IsNullOrEmpty(textFormat.FontFamilyName))
                    control.FontFamily = new FontFamily(textFormat.FontFamilyName);
                if (!string.IsNullOrEmpty(textFormat.ForegroundColor))
                    control.Foreground = new SolidColorBrush(ColorHelperEx.StringToColor(textFormat.ForegroundColor));
            }
        }

        public static SuperTextFormat GetRichTextBlockFormat(RichTextBlock control)
        {
            return new SuperTextFormat()
            {
                FontFamilyName = control.FontFamily.Source,
                FontSize = control.FontSize,
                FontStyle = ConvertFromFontStyle(control.FontStyle),
                FontWeight = ConvertFromFontWeight(control.FontWeight),
                ForegroundColor = ColorHelperEx.ColorToString(((SolidColorBrush)control.Foreground).Color),
            };
        }

        public static SuperTextFormat GetControlFormat(Control control)
        {
            return new SuperTextFormat() { 
                FontFamilyName = control.FontFamily.Source,
                FontSize = control.FontSize,
                FontStyle = ConvertFromFontStyle(control.FontStyle),
                FontWeight = ConvertFromFontWeight(control.FontWeight),
                ForegroundColor =  ColorHelperEx.ColorToString(((SolidColorBrush)control.Foreground).Color),
            };
        }

        public static SuperTextFormat GetTextElementFormat(TextElement control)
        {
            return new SuperTextFormat()
            {
                FontFamilyName = control.FontFamily.Source,
                FontSize = control.FontSize,
                FontStyle = ConvertFromFontStyle(control.FontStyle),
                FontWeight = ConvertFromFontWeight(control.FontWeight),
                ForegroundColor = ColorHelperEx.ColorToString(((SolidColorBrush)control.Foreground).Color),
            };
        }

        public static FontStyle ConvertToFontStyle(SuperFontStyle superFontStyle)
        {
            switch (superFontStyle)
            {
                case SuperFontStyle.Italic:
                    return FontStyle.Italic;
                case SuperFontStyle.Normal:
                default:
                    return FontStyle.Normal;
                case SuperFontStyle.Oblique:
                    return FontStyle.Oblique;
            }
        }

        public static FontWeight ConvertToFontWeight(SuperFontWeight superFontWeight)
        {
            switch (superFontWeight)
            {
                case SuperFontWeight.Black:
                    return FontWeights.Black;
                case SuperFontWeight.Bold:
                    return FontWeights.Bold;
                case SuperFontWeight.ExtraBlack:
                    return FontWeights.ExtraBlack;
                case SuperFontWeight.ExtraBold:
                    return FontWeights.ExtraBold;
                case SuperFontWeight.ExtraLight:
                    return FontWeights.ExtraLight;
                case SuperFontWeight.Light:
                    return FontWeights.Light;
                case SuperFontWeight.Medium:
                    return FontWeights.Medium;
                case SuperFontWeight.Normal:
                default:
                    return FontWeights.Normal;
                case SuperFontWeight.SemiBold:
                    return FontWeights.SemiBold;
                case SuperFontWeight.SemiLight:
                    return FontWeights.SemiLight;
                case SuperFontWeight.Thin:
                    return FontWeights.Thin;
            }
        }

        public static SuperFontStyle ConvertFromFontStyle(FontStyle fontStyle)
        {
            switch (fontStyle)
            {
                case FontStyle.Italic:
                    return SuperFontStyle.Italic;
                case FontStyle.Normal:
                default:
                    return SuperFontStyle.Normal;
                case FontStyle.Oblique:
                    return SuperFontStyle.Oblique;
            }
        }

        public static SuperFontWeight ConvertFromFontWeight(FontWeight fontWeight)
        {
            if (fontWeight.Weight == FontWeights.Black.Weight)
                return SuperFontWeight.Black;
            else if (fontWeight.Weight == FontWeights.Bold.Weight)
                return SuperFontWeight.Bold;
            else if (fontWeight.Weight == FontWeights.ExtraBlack.Weight)
                return SuperFontWeight.ExtraBlack;
            else if (fontWeight.Weight == FontWeights.ExtraBold.Weight)
                return SuperFontWeight.ExtraBold;
            else if (fontWeight.Weight == FontWeights.ExtraLight.Weight)
                return SuperFontWeight.ExtraLight;
            else if (fontWeight.Weight == FontWeights.Light.Weight)
                return SuperFontWeight.Light;
            else if (fontWeight.Weight == FontWeights.Medium.Weight)
                return SuperFontWeight.Medium;
            else if (fontWeight.Weight == FontWeights.Normal.Weight)
                return SuperFontWeight.Normal;
            else if (fontWeight.Weight == FontWeights.SemiBold.Weight)
                return SuperFontWeight.SemiBold;
            else if (fontWeight.Weight == FontWeights.SemiLight.Weight)
                return SuperFontWeight.SemiLight;
            else if (fontWeight.Weight == FontWeights.Thin.Weight)
                return SuperFontWeight.Thin;
            return SuperFontWeight.Normal;
        }

    }
}
